﻿using System.Windows;
using IMap;
using System.Diagnostics;
using System;
using System.Windows.Controls;
using ICoord;

namespace HttpDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MapInitialize();
        }

        private void MapInitialize()
        {
            iMapContainer.Level =(int) this.slider1.Value;
            iMapContainer.CenterLatlong = new LatLong(31.2417, 121.4953);
            iMapContainer.MapCache= CacheType.CacheAndServer;
            iMapContainer.WebMap = MapType.sate;           
            radioWebMap.IsChecked = false;
            chkShowGrid.IsChecked = true;
            iMapContainer.RedrawMap();
            iMapContainer.ScanPoint();
        }

        private void btnZout_Click(object sender, RoutedEventArgs e)
        {
           
            Debug.Print("缩放之前的级别" + iMapContainer.Level.ToString());
            int newLevel=iMapContainer.Level-1;
            if (newLevel <= iMapContainer.MinLevel)
            {
                newLevel = Core.NumClip(newLevel, iMapContainer.MinLevel, iMapContainer.MaxLevel);
                Debug.Print("当前缩放级别" + newLevel.ToString());
                Debug.Print("已经缩小到最小级别了，无法继续缩小");
            }
            else
            {
                iMapContainer.Zoom(newLevel);
            }
            label1.Content = "当前缩放级别" + iMapContainer.Level;
        }

        private void btnZin_Click(object sender, RoutedEventArgs e)
        {
            Debug.Print("缩放之前的级别" + iMapContainer.Level.ToString());
            int newLevel = iMapContainer.Level + 1;
            if (newLevel >= iMapContainer.MaxLevel)
            {
                newLevel = Core.NumClip(newLevel, iMapContainer.MinLevel, iMapContainer.MaxLevel);
                Debug.Print("当前缩放级别" + newLevel.ToString());
                Debug.Print("已经放大到最大级别了，无法继续放大");
            }
            else
            {
                iMapContainer.Zoom(newLevel);
            }
            label1.Content = "当前缩放级别" + iMapContainer.Level;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            iMapContainer.ScanPoint(Convert.ToInt32(textBox1.Text));
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            double x = Convert.ToDouble(textBox2.Text);
            double y = Convert.ToDouble(textBox3.Text);
            MercatorPoint mpt = new MercatorPoint(x, y);
            MessageBoxResult msg = MessageBox.Show("是否将该点标注?", "显示标注", MessageBoxButton.OKCancel);
            if (msg == MessageBoxResult.OK)
            {
                Marker marker = new Marker();
                {
                    iMapContainer.Children.Add(marker);
                    //CoordTrans7Param params7 = new CoordTrans7Param();
                    ////必须知道上海原点经纬度，最好通过其参数将城建坐标转换为经纬度，此处需要测量部帮助
                    //LatLong ll0 = new LatLong(23.065643, 113.303454);
                    //double ppx = Core.Mercator2Pixel(mpt, iMapContainer.Level).X + Core.LatLong2Pixel(ll0, iMapContainer.Level).X;
                    //double ppy = Core.Mercator2Pixel(mpt, iMapContainer.Level).Y + Core.LatLong2Pixel(ll0, iMapContainer.Level).Y;
                    //PixelPoint pp = new PixelPoint(ppx, ppy);
                    //PixelPoint CenterPixelPoint=Core.Mercator2Pixel(iMapContainer.CenterMercatorPoint,iMapContainer.Level);
                    //double ptx = iMapContainer.PixelPoint2LocalPoint(pp, CenterPixelPoint).X;
                    //double pty = iMapContainer.PixelPoint2LocalPoint(pp, CenterPixelPoint).Y;
                    //Canvas.SetLeft(marker, ptx);
                    //Canvas.SetTop(marker, pty);
                }

            }
        }

        private void radioWebMap_Checked(object sender, RoutedEventArgs e)
        {
            if (radioWebMap.IsChecked==true)
            {
                radioSatellite.IsChecked = false;
                if (iMapContainer.layers_manager.Contains(LayerType.Trans))
                {
                    iMapContainer.layers_manager.Remove(LayerType.Trans);
                }
                iMapContainer.WebMap = MapType.web;
                iMapContainer.RedrawMap();           
            }
        }

        private void radioSatellite_Checked(object sender, RoutedEventArgs e)
        {
            if (radioSatellite.IsChecked==true)
            {
                iMapContainer.WebMap = MapType.sate;
                iMapContainer.RedrawMap();
                radioWebMap.IsChecked = false;                
            }
        }

        private void btnSurvey_Click(object sender, RoutedEventArgs e)
        {
            iMapContainer.IsSurveying = true;
            iMapContainer.IsMoving = false;
            iMapContainer.IsLTClicked = false;
        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int oldLevel = (int)e.OldValue;
            int newLevel = (int)e.NewValue;
            Debug.Print("缩放之前的级别" + oldLevel.ToString());
            if (newLevel >= iMapContainer.MaxLevel)
            {
                newLevel = Core.NumClip(newLevel, iMapContainer.MinLevel, iMapContainer.MaxLevel);
                Debug.Print("当前缩放级别" + newLevel.ToString());
                Debug.Print("已经放大到最大级别了，无法继续放大");
            }

            else if (newLevel <= iMapContainer.MinLevel)
            {
                newLevel = Core.NumClip(newLevel, iMapContainer.MinLevel, iMapContainer.MaxLevel);
                Debug.Print("当前缩放级别" + newLevel.ToString());
                Debug.Print("已经缩小到最小级别了，无法继续缩小");
            }
            else
            {
                iMapContainer.Zoom(newLevel);
            }
        }

        private void chkShowGrid_Checked(object sender, RoutedEventArgs e)
        {            
            if (chkShowGrid.IsChecked == true)
            {
                iMapContainer.IsShowGrid = true;
            }
        }

        private void chkShowGrid_Unchecked(object sender, RoutedEventArgs e)
        {
            if (chkShowGrid.IsChecked ==false)
            {
                iMapContainer.IsShowGrid = false;                
            }

        }


    }
}
